package cz.cuni.amis.episodic.lisp.netcreators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class DayPaddingStrategy<T> implements TraceModificationStrategy {

	@NonNull
	T dayStopAction;

	/**
	 * Added to the end of all days.
	 */
	@NonNull
	T[] dayPaddingTrace;
	
	@Getter
	int batchSize = -1;

	@Override
	public <T> List<T[]> modifyTrace(List<T[]> traces, boolean countLast) {
		// find length of days
		int maxDayLength = Integer.MIN_VALUE;
		int dayStart = 0;
		int i = 0;

		// find the longest day
		for (T[] trace : traces) {
			if (trace[0].equals(dayStopAction)) {
				maxDayLength = Math.max(maxDayLength, i - dayStart + 1);
				dayStart = i;
			}
			i++;
		}

		// insert padding to make all days equal in length
		List<T[]> padded = new ArrayList<>(traces.size());
		int currentDayLength = 0;
		
		for (T[] trace : traces.subList(1, traces.size())) {
			currentDayLength++;
			if (trace[0].equals(dayStopAction)) {
				// sleep as the end of a day
				padded.add(trace);
				// add necessary number of end symbols
				for (int j = currentDayLength; j < maxDayLength; j++) {
					padded.add((T[]) dayPaddingTrace);
				}
				currentDayLength = 0;

			} else {
				padded.add(trace);
			}
		}
		
		batchSize = maxDayLength;
		return padded;
	}

	@Override
	public int getDepth() {
		// TODO Auto-generated method stub
		return -1;
	}

}
